package com.jf.yytgd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf.yytgd.converter.PageConverter;
import com.jf.yytgd.converter.ScienceArticleConverter;
import com.jf.yytgd.dao.ScienceArticleMapper;
import com.jf.yytgd.entity.bean.ScienceArticle;
import com.jf.yytgd.entity.dto.like.LikeDTO;
import com.jf.yytgd.entity.dto.like.LikeExistDTO;
import com.jf.yytgd.entity.dto.scienceArticle.ScienceArticleBarDTO;
import com.jf.yytgd.entity.dto.scienceArticle.ScienceArticleFullDTO;
import com.jf.yytgd.service.LikeService;
import com.jf.yytgd.service.ScienceArticleService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description: 科普文章服务层实现
 * User:22954
 * Date:2025-01-13
 * Time:13:00
 */
@Transactional(isolation = Isolation.READ_COMMITTED)
@Service
public class ScienceArticleServiceImpl extends ServiceImpl<ScienceArticleMapper, ScienceArticle> implements ScienceArticleService {
    @Resource
    private ScienceArticleMapper scienceArticleMapper;
    @Resource
    private LikeService likeService;

    @Override
    public ScienceArticleFullDTO browse(Integer articleId) {
        ScienceArticle scienceArticle = scienceArticleMapper.getScienceArticleById(articleId);
        scienceArticle.setViews(scienceArticle.getViews() + 1);
        scienceArticleMapper.updateById(scienceArticle);
        return ScienceArticleConverter.INSTANCE.toFullDto(scienceArticle);
    }

    @Override
    public Page<ScienceArticleBarDTO> pageBar(Integer pageNo, Integer pageSize, String search, String order) {
        Page<ScienceArticle> page = new Page<>(pageNo, pageSize);
        QueryWrapper<ScienceArticle> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(search)) {
            queryWrapper.like("title", search);
        }
        if (StringUtils.hasText(order)) {
            queryWrapper.orderByDesc(order);
        }
        page = page(page, queryWrapper);
        List<ScienceArticleBarDTO> dtoRecords = new ArrayList<>();
        for (ScienceArticle record : page.getRecords()) {
            ScienceArticleBarDTO scienceArticleBarDTO = ScienceArticleConverter.INSTANCE.toBarDto(record);
            dtoRecords.add(scienceArticleBarDTO);
        }
        return PageConverter.INSTANCE.convert(page, dtoRecords);
    }

    @Override
    public void like(LikeDTO likeDTO) {
        likeDTO.setRelatedType("ScienceArticle");
        boolean isLike = likeService.like(likeDTO);
        ScienceArticle scienceArticle = scienceArticleMapper.getScienceArticleById(likeDTO.getRelatedId());
        if (isLike) {
            scienceArticle.setLikes(scienceArticle.getLikes() + 1);
        } else {
            scienceArticle.setLikes(scienceArticle.getLikes() - 1);
        }
        scienceArticleMapper.updateById(scienceArticle);
    }

    @Override
    public boolean isLike(LikeExistDTO likeDTO) {
        likeDTO.setRelatedType("ScienceArticle");
        return likeService.isLike(likeDTO);
    }
}
